package com.luoxiang.socialandroid.dialog

import android.content.Context
import android.util.Log
import android.widget.Toast
import androidx.core.content.ContextCompat
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.luoxiang.socialandroid.R
import com.luoxiang.socialandroid.databinding.DialogMassageSuspensionBinding
import com.lxj.xpopup.core.PositionPopupView
import com.netease.nimlib.sdk.NIMClient
import com.netease.nimlib.sdk.msg.model.IMMessage
import com.netease.nimlib.sdk.uinfo.UserService
import com.netease.nimlib.sdk.uinfo.constant.GenderEnum
import com.netease.nimlib.sdk.uinfo.model.NimUserInfo
import com.netease.yunxin.kit.chatkit.ui.custom.GiftAttachment
import com.netease.yunxin.kit.corekit.im.model.UserInfo
import com.netease.yunxin.kit.corekit.im.utils.RouterConstant.CHAT_KRY
import com.netease.yunxin.kit.corekit.im.utils.RouterConstant.PATH_CHAT_P2P_PAGE
import com.netease.yunxin.kit.corekit.route.XKitRouter.withKey
import java.text.SimpleDateFormat
import java.util.*

class MassageSuspensionDialog(context: Context) : PositionPopupView(context) {
    private lateinit var mViewBinding: DialogMassageSuspensionBinding
    private lateinit var data: IMMessage

    override fun getImplLayoutId() = R.layout.dialog_massage_suspension

    override fun onCreate() {
        super.onCreate()
        delayDismiss(3000L)
        mViewBinding = DialogMassageSuspensionBinding.bind(popupImplView)
        mViewBinding.textFromName.text = data.fromNick
        var user: NimUserInfo? = null
        var count = 1
        while(user == null){
            Log.d("userService","第$count 次获取")
            user = NIMClient.getService(UserService::class.java).getUserInfo(data.fromAccount)
            count++
        }
        Glide.with(context)
            .load(user.avatar)
            .apply(RequestOptions.bitmapTransform(CircleCrop()))
            .into(mViewBinding.imageHead)
        if (user.genderEnum == GenderEnum.MALE) {
            mViewBinding.imageView5.setImageDrawable(
                ContextCompat.getDrawable(
                    context,
                    R.mipmap.icon_nansheng
                )
            )
            mViewBinding.view6.background =
                ContextCompat.getDrawable(context, R.drawable.shape_tc_txbg)
        } else if (user.genderEnum == GenderEnum.FEMALE) {
            mViewBinding.imageView5.setImageDrawable(
                ContextCompat.getDrawable(
                    context,
                    R.mipmap.icon_nvsheng
                )
            )
            mViewBinding.view6.background =
                ContextCompat.getDrawable(context, R.drawable.shape_tc_txbgnv)
        }
        val birthday = user.birthday
        if (!birthday.isNullOrEmpty()) {
            val birthdayYear = birthday.split('-')[0].toInt()
            val nowYear = SimpleDateFormat("yyyy", Locale.CHINA)
                .format(System.currentTimeMillis()).toInt()
            mViewBinding.textAge.text = (nowYear - birthdayYear).toString()
        }
        if (data.attachment is GiftAttachment) {
            mViewBinding.textContent.text = context.getString(R.string.mi_liwuxiaoxi)
        } else {
            mViewBinding.textContent.text = data.content
        }
        mViewBinding.textReply.setOnClickListener {
            withKey(PATH_CHAT_P2P_PAGE).withParam(
                CHAT_KRY,
                UserInfo(user.account, user.name, user.avatar)
            ).withContext(context).navigate()
        }
    }

    fun setData(data: IMMessage) {
        this.data = data
    }
}